trigger updateAccounts on Contact (after delete, after insert, after update)
{
    /* The trigger updates the fields in Accounts
       which are the the roll up summary fields for the fields
       of Contacts.
     */
     
    //New Approach After 15th May 2012....
    Set<id> currentEmployerIds = new Set<id>();
    
    List<Account> accountUpdationList = new List<Account>();
    
    if (Trigger.isInsert || Trigger.isUpdate) 
    {
        for (Contact item : Trigger.new) 
        {
            if(item.Current_Employer__c != null)
                currentEmployerIds.add(item.Current_Employer__c);
             
        }
        
    }
    
    // If the Contact is being deleted, then the associated Account needs to be updated.     
    // If the Contact is being updated, then both the old and new Accounts are processed.
      
    if (Trigger.isUpdate || Trigger.isDelete)
    {
        for (Contact item : Trigger.old) 
        {
            if(item.Current_Employer__c != null)
                currentEmployerIds.add(item.Current_Employer__c);
               
        }   
        
    }
    
    //Now the Logic Begins...
    if(currentEmployerIds.size() > 0) 
    { 
        //We query because of the Old Trigger Values and the New Trigger Values...
        List<Contact> contactObjectList = new List<Contact>([select id,Account.Id,Current_Employer__c,Legacy_Visa_Type__c,Visa_Validity_Status__c,Visa_Package__c,Passport_Status__c,Passport_Movement_Status__c from Contact where Current_Employer__c IN: currentEmployerIds]);          
        
        //Store the Account and Account ids in a List...
        List<Account> accountObjectList = new List<Account>([select id,Total_Number_of_Absconding_Visa__c,Cancel_Residence_Without_Doc_visa_quota__c,Total_Number_of_Expired_Passport__c,Total_Number_of_Passport_Outside__c,Cancelled_Residence_visa_quota__c,Visit_Visa_Quota__c,Expired_Residence_visa_quota__c,Residence_Visa_Quota_Available__c from Account where id IN: currentEmployerIds]);                       
        
        //Now Traverse the Account Object Maps...
        for(Account accountObject : accountObjectList)
        {
            //Fist Reset all the values...
            accountObject.Residence_Visa_Quota_Used__c = 0;
            accountObject.Visit_Visa_Quota__c = 0;
            accountObject.Expired_Residence_visa_quota__c = 0;
            accountObject.Cancelled_Residence_visa_quota__c = 0;
            
            accountObject.Total_Number_of_Absconding_Visa__c = 0;
            accountObject.Cancel_Residence_Without_Doc_visa_quota__c = 0;
            accountObject.Total_Number_of_Expired_Passport__c = 0;
            accountObject.Total_Number_of_Passport_Outside__c = 0;
                
            //Iterate through all Contacts to get the childs of the currrent Account and add the corresponding fields of child together to form a roll up summary filed in the parent Account   
            for(Contact contactObject :  contactObjectList)
            {
            	
            	
                //For Visit Visa Used....
                if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Visa_Package__c == 'Visit Visa'))      
                {
                    accountObject.Visit_Visa_Quota__c += 1;         
                }
                //For Cancelled Residence Visa Quota Used...
                else if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Visa_Package__c == 'Employment' || contactObject.Visa_Package__c == 'Transfer') && (contactObject.Visa_Validity_Status__c == 'Cancelled') && (contactObject.Legacy_Visa_Type__c != 'Entry Permit'))
                {
                    accountObject.Cancelled_Residence_visa_quota__c += 1;
                }
                
                //For Expired Residence Visa Quota Used...
                else if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Visa_Package__c == 'Employment' || contactObject.Visa_Package__c == 'Transfer') && (contactObject.Visa_Validity_Status__c == 'Expired') && (contactObject.Legacy_Visa_Type__c != 'Entry Permit'))
                {
                    accountObject.Expired_Residence_visa_quota__c += 1;
                    //Change Request to Count expired visa in Visa Quota Used...
	                accountObject.Residence_Visa_Quota_Used__c += 1;
                }
                //For Residence Visa Quota Used...
                else if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Visa_Package__c == 'Employment' || contactObject.Visa_Package__c == 'Transfer') && (contactObject.Visa_Validity_Status__c != 'Cancelled' && contactObject.Visa_Validity_Status__c != 'Unavailable' && contactObject.Visa_Validity_Status__c != 'Absconding' && contactObject.Visa_Validity_Status__c != 'Cancelled with Absconding'))
                {
                    accountObject.Residence_Visa_Quota_Used__c += 1;
                }
                
                //For Absconding... (Work FIne till 21/10/2012) For the New change Request...
            	/*else if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Legacy_Visa_Type__c == 'Absconding' || contactObject.Visa_Validity_Status__c == 'Absconding' || contactObject.Visa_Validity_Status__c == 'Cancelled with Absconding'))      
                {
                    accountObject.Total_Number_of_Absconding_Visa__c += 1;         
                }*/
                
                else if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Legacy_Visa_Type__c == 'Absconding' || contactObject.Visa_Validity_Status__c == 'Absconding'))      
                {
                    accountObject.Total_Number_of_Absconding_Visa__c += 1;         
                }
                
                else if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Visa_Validity_Status__c == 'Cancelled with Absconding'))      
                {
                    accountObject.Cancelled_Residence_visa_quota__c += 1;         
                }
                
                
                //For Cancelled Without Document Quota Used...
                if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Visa_Package__c == 'Employment' || contactObject.Visa_Package__c == 'Transfer') && (contactObject.Visa_Validity_Status__c == 'Cancellation Without Exist') && (contactObject.Legacy_Visa_Type__c != 'Entry Permit'))
                {
                    accountObject.Cancel_Residence_Without_Doc_visa_quota__c += 1;
                }
                
                //*************************** For passport Information ********************
                if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Passport_Status__c == 'Expired'))      
                {
                    accountObject.Total_Number_of_Expired_Passport__c += 1;         
                }
                
                //For Passport Movement Status...
                if((contactObject.Current_Employer__c == accountObject.id) && (contactObject.Passport_Movement_Status__c == 'Out'))
                {
                    accountObject.Total_Number_of_Passport_Outside__c += 1;
                }
                
            }
            
            
            System.debug('The Residence Visa Quota => ' + accountObject.Residence_Visa_Quota_Used__c );
            System.debug('The Expired Residence Visa Quota => '+accountObject.Expired_Residence_visa_quota__c );
            System.debug('The Cancelled Visa Quota => '+ accountObject.Cancelled_Residence_visa_quota__c);
            System.debug('The Visit Visa Quota => '+accountObject.Visit_Visa_Quota__c );
            //Adding the Account in Account Update List...
            accountUpdationList.add(accountObject);  
            
        }
        
        
        
        //Update the Accounts... 
        if(accountUpdationList.size() > 0)
            update(accountUpdationList);
        
            
    }
    
    //Previous Approach Before 15 May 2012... 
    /*Set<id> Accns = new Set<id>();

    List<Account> AccountsToBeUpdated = new List<Account>();
    
    
    //New Approach After 15th May 2012....
    if (Trigger.isInsert || Trigger.isUpdate) 
    {
        for (Contact item : Trigger.new) 
        {
            if(item.Current_Employer__c != null)
                Accns.add(item.Current_Employer__c);
               
        }
    }
    
    //Previous Approach Before 15 May 2012...
    // creating a set of Ids for Accounts 
    /*if (Trigger.isInsert || Trigger.isUpdate) {
        for (Contact item : Trigger.new) {
            if(item.AccountId != null)
                Accns.add(item.AccountId);
               
        }
    }
     //  If the Contact is being deleted, then the associated Account needs to be updated.     
    // If the Contact is being updated, then both the old and new Accounts are processed.
      
    if (Trigger.isUpdate || Trigger.isDelete)
    {
        //Previous Approach Before 15 May 2012...
        /* for (Contact item : Trigger.old){
              if(item.AccountId != null)
                Accns.add(item.AccountId); 
               }
       //New Approach After 15th May 2012....
        for (Contact item : Trigger.old) 
        {
            if(item.Current_Employer__c != null)
                Accns.add(item.Current_Employer__c);
               
        }
    }
     
    if (Trigger.isInsert || Trigger.isUpdate || Trigger.isDelete) 
    {
        if(Accns.size() > 0) 
        {    
            //New Approach After 15th May 2012....
            List<Contact> con = [select id,Account.Id,Current_Employer__c,Visa_Validity_Status__c,Contact_Type__c from Contact where Current_Employer__c IN: Accns]; 
            //Previous Approach Before 15 May 2012...
            //List<Contact> con = [select id,Account.Id,Visa_Validity_Status__c,Contact_Type__c from Contact where Account.Id IN: Accns];         
            
            //Store the Account and Account ids in a map
            Map<Id,Account> acc = new Map<Id,Account>([select id,Residence_Visa_Quota_Available__c from Account where id IN: Accns]);                       
  
            //Iterate through all the Account ids. 
            for(Id accId:Accns)
            {
                Account parentAccn = acc.get(accId);
                parentAccn.Residence_Visa_Quota_Used__c = 0;
                parentAccn.Visit_Visa_Quota__c = 0;
                parentAccn.Expired_Residence_visa_quota__c = 0;
                parentAccn.Cancelled_Residence_visa_quota__c = 0;
                
                //Iterate through all Contacts to get the childs of the currrent Account and add the corresponding fields of child together to form a roll up summary filed in the parent Account     
                for(integer i=0; i<con.size(); i++)
                {
                    
                    if(con.get(i).Current_Employer__c == accId && con.get(i).Contact_Type__c == 'Visitor')
                    {
                        parentAccn.Visit_Visa_Quota__c += 1;     
                    }
                    if(con.get(i).Current_Employer__c == accId && con.get(i).Visa_Validity_Status__c == 'Expired'&& con.get(i).Contact_Type__c != 'Visitor')
                    {
                        parentAccn.Expired_Residence_visa_quota__c += 1;     
                    } 
                    if(con.get(i).Current_Employer__c == accId && con.get(i).Visa_Validity_Status__c == 'Cancelled' && con.get(i).Contact_Type__c != 'Visitor')
                    {
                        parentAccn.Cancelled_Residence_visa_quota__c += 1;     
                    }
                    if((con.get(i).Current_Employer__c == accId) && (con.get(i).Visa_Validity_Status__c == 'Valid' || con.get(i).Visa_Validity_Status__c == 'Expired' || con.get(i).Visa_Validity_Status__c == 'Cancellation Without Print' || con.get(i).Visa_Validity_Status__c == 'Employee - Under Process') && (con.get(i).Contact_Type__c == 'Employee' || con.get(i).Contact_Type__c == 'Manager' || con.get(i).Contact_Type__c == 'Shareholder' || con.get(i).Contact_Type__c == 'Employee - Under Process'))
                    {
                        parentAccn.Residence_Visa_Quota_Used__c += 1;     
                    }
                                        
                    //Previous Approach Before 15 May 2012...
                    if(con.get(i).AccountId == accId && con.get(i).Contact_Type__c == 'Visitor')
                    {
                        parentAccn.Visit_Visa_Quota__c += 1;     
                    }
                    if(con.get(i).AccountId == accId && con.get(i).Visa_Validity_Status__c == 'Expired'&& con.get(i).Contact_Type__c != 'Visitor')
                    {
                        parentAccn.Expired_Residence_visa_quota__c += 1;     
                    } 
                    if(con.get(i).AccountId == accId && con.get(i).Visa_Validity_Status__c == 'Cancelled' && con.get(i).Contact_Type__c != 'Visitor')
                    {
                        parentAccn.Cancelled_Residence_visa_quota__c += 1;     
                    }
                    if(con.get(i).AccountId == accId && con.get(i).Visa_Validity_Status__c == 'Valid' && (con.get(i).Contact_Type__c == 'Employee' || con.get(i).Contact_Type__c == 'Manager' || con.get(i).Contact_Type__c == 'Shareholder' || con.get(i).Contact_Type__c == 'Employee - Under Process'))
                    {
                        parentAccn.Residence_Visa_Quota_Used__c += 1;     
                    }
                    if(con.get(i).AccountId == accId && con.get(i).Visa_Validity_Status__c == 'Expired' && (con.get(i).Contact_Type__c == 'Employee' || con.get(i).Contact_Type__c == 'Manager' || con.get(i).Contact_Type__c == 'Shareholder' || con.get(i).Contact_Type__c == 'Employee - Under Process'))
                    {
                        parentAccn.Residence_Visa_Quota_Used__c += 1;     
                    }
                    if(con.get(i).AccountId == accId && con.get(i).Visa_Validity_Status__c == 'Cancellation Without Print' && (con.get(i).Contact_Type__c == 'Employee' || con.get(i).Contact_Type__c == 'Manager' || con.get(i).Contact_Type__c == 'Shareholder' || con.get(i).Contact_Type__c == 'Employee - Under Process'))
                    {
                        parentAccn.Residence_Visa_Quota_Used__c += 1;     
                    }     
                    if(con.get(i).AccountId == accId && con.get(i).Visa_Validity_Status__c == 'Employee - Under Process' && (con.get(i).Contact_Type__c == 'Employee' || con.get(i).Contact_Type__c == 'Manager' || con.get(i).Contact_Type__c == 'Shareholder' || con.get(i).Contact_Type__c == 'Employee - Under Process'))
                    {
                        parentAccn.Residence_Visa_Quota_Used__c += 1;     
                    }          
                }
                
                
                AccountsToBeUpdated.add(parentAccn);  
            } 
        }
    }
    // Insert the updated Accounts 
    if(AccountsToBeUpdated != null)
      upsert(AccountsToBeUpdated);*/
   

}